#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
pthread_mutex_t mutex_lock=PTHREAD_MUTEX_INITIALIZER;
int g_count;
void* thread_func(void* data){
char* thread_name=(char*)data;
printf("[%s] Ready for Thread\n", thread_name);
sleep(1);
pthread_mutex_lock(&mutex_lock);
printf("[%s] Start Counting\n", thread_name);
for(int i=0;i<1000;i++){
g_count++;
printf("%d\n", g_count);
}
printf("[%s] Finish Counting\n", thread_name);
pthread_mutex_unlock(&mutex_lock);
return NULL;
}
int main(void){
pthread_t p_thread1, p_thread2;
pthread_create(&p_thread1, NULL, thread_func, (void*)"Thread 1");
pthread_create(&p_thread2, NULL, thread_func, (void*)"Thread 2");
printf("\n\nWaiting for Join\n\n");
pthread_join(p_thread1, NULL);
pthread_join(p_thread2, NULL);
printf("pthread exit\n");
pthread_exit(NULL);
}